<?php
class Message implements \arrayaccess,\Iterator,\Countable 
{
    protected static $_db=null;
    private $stmt;
    private $valid;
    private $key;
    private $value;
    private $pagesize;
    private $startpage;
    const sql_createdb='
CREATE  TABLE  IF NOT EXISTS "Message" 
(
    "index" INTEGER PRIMARY KEY  AUTOINCREMENT  NOT NULL ,
    "name" VARCHAR, 
    "title" VARCHAR, 
    "mail" VARCHAR, 
    "url" VARCHAR, 
    "body" TEXT, 
    "ip" VARCHAR, 
    "time" INTEGER DEFAULT CURRENT_TIMESTAMP
)
    ';
    public function __construct($pagesize=50,$startpage=0)
    {
        $this->pagesize=(int)$pagesize;
        $this->startpage=(int)$startpage;
        self::init();
    }
    public function offsetSet($offset,$value)
    {
        $sql='
INSERT INTO "Message"
(
    "name", 
    "title", 
    "mail", 
    "url", 
    "body", 
    "ip"
)
VALUES
(
    :name, 
    :title, 
    :mail, 
    :url, 
    :body, 
    :ip
)
        ';
        $this->stmt = self::$_db->prepare($sql);
        if ($this->stmt->execute($value))
        {
            $this->rewind();
        }
        else
        {
            new ErrorLog($this->stmt->errorInfo());
        }
    }
    public function offsetExists($offset) 
    {
        return true;
    }
    public function offsetUnset($offset)
    {
		$sql='DELETE FROM Message WHERE "index" = :index LIMIT 1';
        $stmt = self::$_db->prepare($sql);
        if ($stmt->execute(array('index'=>$offset)))
        {
            $this->rewind();
        }
        else
        {
            new ErrorLog($this->stmt->errorInfo());
        }
    }
    public function offsetGet($offset) 
    {
        return Null;
    }
    public function current()
    {
        $r=array();
        $r['value']=$this->value;
        $r['ResponseMessage']=new ResponseMessage($this->key,self::$_db);
        return $r;
    }
    public function key()
    {
        return $this->key;
    }
    public function next()
    {
        if($row = $this->stmt->fetch(database::FETCH_ASSOC))
        {
            $this->key=$row['index'];
            $this->value=$row;
            $this->valid=true;
        }
        else
        {
            $this->valid=false;
        }
    }
    public function rewind()
    {
        $sql='
select * from Message order by "index" desc limit :startpage ,:pagesize
        ';
        $this->stmt = self::$_db->prepare($sql);
        if ($this->stmt->execute(array('startpage'=>$this->startpage,'pagesize'=>$this->pagesize)))
        {
            $this->next();
        }
        else
        {
            new ErrorLog($this->stmt->errorInfo());
        }
    }
    public function valid()
    {
        return $this->valid;
    }
    public function count()
    {
        $sql='select count(*) from Message';
        $stmt = self::$_db->prepare($sql);
        $stmt->execute();
        return $stmt->fetchColumn();
    }
    protected static function init()
    {
        if(self::$_db instanceof database)
        {
            return self::$_db;
        }
        else
        {
            self::$_db= new database();
        }
    }
    public static function initdb()
    {
        self::init();
        self::$_db->exec(self::sql_createdb);
        ResponseMessage::initdb();
        self::$_db= new database();
    }
}
